# 每个月第一个交易日买100股，年底全部卖完
import pandas as pd

df = pd.read_csv('./files/前复权./files/茅台.csv', index_col='date', parse_dates=['date'])

df = df['2001-01':'2017-07']
price_last = df['open'].iloc[-1]

df_monthly = df.resample('ME').first()
df_yearly = df.resample('YE').last()[:-1]

cost_money = 0
hold = 0
for year in range(2001, 2018):
  num = df_monthly[df_monthly.index.year == year]
  
  cost_money += num['open'].sum()*100
  hold += len(num['open'])*100
  if year != 2017:
    cost_money -= df_yearly[df_yearly.index.year == year]['open'].iloc[0] * hold
    hold = 0

cost_money -= hold * price_last

print(-cost_money)
